package io.nads.listener;

import io.nads.config.RabbitConfig;
import io.nads.entity.UserChatMessage;
import io.nads.mapper.ChatMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

/**
 * @Author: shzhu
 * @Date: 2025/9/25 00:48
 * @Description:
 */

@Component
@Slf4j
public class ChatMessageListener {

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    @RabbitListener(queues = RabbitConfig.QUEUE_NAME, concurrency = "3-10")
    public void receiveMessage(UserChatMessage message) {
        System.out.println("收到消息: " + message);
        try {
            chatMessageMapper.insert(message); // 写入数据库
        } catch (DuplicateKeyException e) {
            log.warn(e.getMessage());
            log.warn(message + "重复");

        }
    }
}

